<?php
include_once '\lib\date\DateUtil.php';

class Controller{
	var $project_table;
	var $tablePrefix="TimeTracking";
	
	function Controller(){
		global $wpdb;
		$this->project_table = $wpdb->prefix .$this->tablePrefix;
		
	}
	
	function insert($data){
		global $wpdb;
		$query = "INSERT INTO $this->project_table (userId,place,description,date,workedHour) VALUES (".$data['userId'].",'".$date['place']."','".$data['description']."','".$data['date']."',".$data['workedHour'].")";
		
		return $wpdb->query($query);
	}
	
	function update($data){
		global $wpdb;
		$query = "UPDATE $project_table SET 	user = '".$data['user']."',
												place = '".$date['place']."',
												description = '".$data['description']."',
												date = '".$data['date']."',
												workedHour = '".$data['workedHour']."') WHERE id = ".$data['id'];
		return $wpdb->query($query);
	}
	
	function delete($data){
		global $wpdb;
		$query = "DELETE FROM $this->project_table WHERE id = ".$data['id'];
		return $wpdb->query($query);
	}
	
	function deletePerId($id){
		global $wpdb;
		$query = "DELETE FROM $this->project_table WHERE id = $id";
		fb($query,'query');
		$wpdb->query($query);
	}
	
	function selectPerDateAndUser($date,$user){
		global $wpdb;
		$query = "SELECT * FROM $this->project_table WHERE userId=$user AND date='$date'";
		return $wpdb->get_results($query,ARRAY_A);
	}
	
	function showPerUser($user){
		global $wpdb;
		$query = "SELECT * FROM $this->project_table WHERE userId=$user";
		return $wpdb->get_results($query,ARRAY_A);
	}
	
	function selectPerMonthAndUser($month,$year,$user){
		global $wpdb;
		$firstDay = DateUtil::firstDayOfMonth($month,$year);
		$lastDay= DateUtil::lastDayOfMonth($month,$year);
		$query = "SELECT * FROM $this->project_table WHERE userId=$user AND date between '$firstDay' AND '$lastDay' order by date ASC";
		return $wpdb->get_results($query,ARRAY_A);
	}
	
	function installQuery(){
		global $wpdb;
		$query = "CREATE TABLE  $this->project_table (
								`id` INT NOT NULL ,
								`userId` INT NOT NULL ,
								`place` VARCHAR( 256 ) NOT NULL ,
								`description` VARCHAR( 256 ) NOT NULL ,
								`workedHour` INT NOT NULL ,
								`date` DATE NOT NULL ,
								PRIMARY KEY (  `id` )
								) ENGINE = MYISAM ;";
		//TODO:: AGGIUNGERE SU ID AUTOINCREMENT
		//ALTER TABLE  `wp_timetracking` CHANGE  `id`  `id` INT( 11 ) NOT NULL AUTO_INCREMENT
		
		$wpdb->query($query);	
	}
}
?>